﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Waaahsabi.Controls.FileSystem
{
	public class GatewayFileManager : Control, IFileManager
	{
		private WrapPanel _itemContainer;
		//private TextBlock txtTotalCapacity;
		//private TextBlock txtAvailableCapacity;
// ReSharper disable UnaccessedField.Local
		private ProgressBar _prgCapacity;
// ReSharper restore UnaccessedField.Local
		private TextBox _txtFileSearch;
		private Button _btnRefresh;

		private readonly ObservableCollection<GatewayFileItem> _fileItems;
		public ObservableCollection<GatewayFileItem> FileItems
		{
			get { return _fileItems; }
		}

		public ulong DirectoryId
		{
			get;
			set;
		}

		#region TotalCapacity DependencyProperty
		public static readonly DependencyProperty TotalCapacityProperty =
		DependencyProperty.Register
			(
			"TotalCapacity",
			typeof(double),
			typeof(GatewayFileManager),
			new PropertyMetadata(null)
			);

		public double TotalCapacity
		{
			get
			{
				return (double)GetValue(TotalCapacityProperty);
			}
			set
			{
				SetValue(TotalCapacityProperty, value);
			}
		}
		#endregion

		#region AvailableCapacity DependencyProperty
		public static readonly DependencyProperty AvailableCapacityProperty =
		DependencyProperty.Register
			(
			"AvailableCapacity",
			typeof(double),
			typeof(GatewayFileManager),
			new PropertyMetadata(null)
			);

		public double AvailableCapacity
		{
			get
			{
				return (double) GetValue(AvailableCapacityProperty);
			}
			private set
			{
				SetValue(AvailableCapacityProperty, value);
			}
		}
		#endregion

		#region UsedCapacity DependencyProperty
		public static readonly DependencyProperty UsedCapacityProperty =
		DependencyProperty.Register
			(
			"UsedCapacity",
			typeof(double),
			typeof(GatewayFileManager),
			new PropertyMetadata(null)
			);

		public double UsedCapacity
		{
			get
			{
				return (double)GetValue(UsedCapacityProperty);
			}
			private set
			{
				SetValue(UsedCapacityProperty, value);
			}
		}
		#endregion

		public string Filter
		{
			get
			{
				if(_txtFileSearch !=null)
					return _txtFileSearch.Text;
				return null;
			}

			set
			{
				if (_txtFileSearch != null)
					_txtFileSearch.Text = value;
			}
		}


		public event EventHandler OnRefreshRequested;

		public void RefreshContent()
		{
			if (OnRefreshRequested != null)
				OnRefreshRequested(this, null);
		}

		public void Refresh()
		{
			if (_itemContainer != null)
			{
				_itemContainer.Children.Clear();
				_fileItems.OrderBy(it => it.FileName).ToList().ForEach(it => _itemContainer.Children.Add(it));
				UsedCapacity = (_fileItems.Sum(it => it.FileSize));
				AvailableCapacity = TotalCapacity - UsedCapacity;
				TxtFileSearchTextChanged(this, null);
			}
		}


		#region IsCondensed DependencyProperty
		public static readonly DependencyProperty IsCondensedProperty =
		DependencyProperty.Register
			(
			"IsCondensed",
			typeof(bool),
			typeof(GatewayFileManager),
			new PropertyMetadata(null)
			);

		public bool IsCondensed
		{
			get { return (bool)GetValue(IsCondensedProperty); }
			set { SetValue(IsCondensedProperty, value); }
		}
		#endregion

		public GatewayFileManager()
		{
			DefaultStyleKey = typeof(GatewayFileManager);
			_fileItems = new ObservableCollection<GatewayFileItem>();
			_fileItems.CollectionChanged += FileItemsCollectionChanged;
		}

		void FileItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if(e.Action == NotifyCollectionChangedAction.Add)
			{
				foreach(var i in e.NewItems)
				{
					if(i is GatewayFileItem)
					{
						GatewayFileItem fi = i as GatewayFileItem;
						fi.Click += FileItemClick;
					}
				}
			}
			Refresh();
		}

		void FileItemClick(object sender, EventArgs e)
		{
			if(sender is GatewayFileItem)
			{
				foreach (var item in _fileItems)
					VisualStateManager.GoToState(item, "Idle", true);
				GatewayFileItem fi = sender as GatewayFileItem;
				if (fi.IsSelected)
					VisualStateManager.GoToState(fi, "Selected", true);
			}
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_itemContainer = GetTemplateChild("itemContainer") as WrapPanel;
			_prgCapacity = GetTemplateChild("prgCapacity") as ProgressBar;
			_btnRefresh = GetTemplateChild("btnRefresh") as Button;
			if (_btnRefresh != null)
				_btnRefresh.Click += (s, e) => RefreshContent();
			_txtFileSearch = GetTemplateChild("txtFileSearch") as TextBox;
			if(_txtFileSearch != null)
			{
				_txtFileSearch.TextChanged += TxtFileSearchTextChanged;
				_txtFileSearch.KeyDown += TxtFileSearchKeyDown;
				_txtFileSearch.GotFocus += (s, e) => _txtFileSearch.SelectAll();
			}
			if (IsCondensed)
				SetCondensed();
			RefreshContent();
		}

		private void TxtFileSearchKeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key != Key.Enter)
				return;
		}

		private void TxtFileSearchTextChanged(object sender, TextChangedEventArgs e)
		{
			if (_txtFileSearch.Text.Length < 1)
				_itemContainer.Children.OfType<GatewayFileItem>().ToList().ForEach(it => it.Visibility = Visibility.Visible);
			else
			{
				foreach (GatewayFileItem i in _itemContainer.Children.OfType<GatewayFileItem>())
				{
					if (i.FileName.ToLower().Contains(_txtFileSearch.Text.ToLower()))
						i.Visibility = Visibility.Visible;
					else
						i.Visibility = Visibility.Collapsed;
				}
			}
		}

		public void SetCondensed()
		{
			foreach (var i in _fileItems)
				i.Mode = FileItemDisplayMode.Condensed;
		}

		public void SetLarge()
		{
			foreach (var i in _fileItems)
				i.Mode = FileItemDisplayMode.Large;
		}
	}
}
 